%{
##########################################################################
##########################################################################
Peter Maxted

Wrapper (main) code to run cake-eating model for different rCC calibrations

***This code is based heavily on Achdou et al. (2017) codes***
   Underlying code from Lee and Maxted; Updated for paper here

##########################################################################
##########################################################################
%}

clear all;


%% Solve Model
rCC = 0.30;
model_calib;
solve_HJB;

VH = V; 
bdotH = bdot;
cH = c;
bH = b;

rCC = 0.15;
model_calib;
solve_HJB;

VL = V; 
bdotL = bdot;
cL = c;
bL = b;

bSSH = find(bdotH(:,2) > 0, 1, 'last') + 1;
bSSL = find(bdotL(:,2) > 0, 1, 'last') + 1;


%% Plot Output
 fig = figure; grid on; hold on;
    l1 = plot(bH, cH(:,2), 'color', 'red', 'linewidth', 4);
    l2 = plot(bL, cL(:,2), 'color', [.4 .4 .4], 'linewidth', 3.5, 'linestyle', '--');
    xlim([-0.3 0.3+eps]);
    set(gca, 'fontsize', 18); ylim([0.8,1.3])
    xlabel('Liquid Wealth ($b$)', 'fontsize', 22, 'interpreter', 'latex'); 
    ylabel('Consumption', 'fontsize', 22, 'interpreter', 'latex');
    legend([l1,l2], ["$r^{cc} = 30\%$", "$r^{cc} = 15\%$"], 'interpreter', 'latex', 'fontsize', 22, 'location', 'southeast');
 saveas(fig, ['figs/Figure7_consumption.png'])



 fig = figure; grid on; hold on;
    l1 = plot(bH, bdotH(:,2), 'color', 'red', 'linewidth', 4);
    l2 = plot(bL, bdotL(:,2), 'color', [.4 .4 .4], 'linewidth', 3.5, 'linestyle', '--');
    line([-3.5 5],[0 0], 'color', 'black', 'linewidth', 1.5);
    xlim([-0.3 0.3+eps]);
    yl = ylim;
    tmp = line([bH(bSSH),bH(bSSH)],[yl(1),yl(2)], 'color', 'red', 'linestyle', ':', 'linewidth', 2); uistack(tmp, 'bottom');
    tmp = line([bL(bSSL),bL(bSSL)],[yl(1),yl(2)], 'color', [.4 .4 .4], 'linestyle', ':', 'linewidth', 2); uistack(tmp, 'bottom');
    set(gca, 'fontsize', 18); ylim(yl)
    xlabel('Liquid Wealth ($b$)', 'fontsize', 22, 'interpreter', 'latex'); 
    ylabel('Saving', 'fontsize', 22, 'interpreter', 'latex');
 saveas(fig, ['figs/Figure7_saving.png'])



 fig = figure; grid on; hold on;
    l1 = plot(bH, VH(:,2), 'color', 'red', 'linewidth', 4);
    l2 = plot(bL, VL(:,2), 'color', [.4 .4 .4], 'linewidth', 3.5, 'linestyle', '--');
    xlim([-0.3 0.3+eps]);
    yl = ylim;
    set(gca, 'fontsize', 18); ylim(yl)
    xlabel('Liquid Wealth ($b$)', 'fontsize', 22, 'interpreter', 'latex'); 
    ylabel('Continuation-Value Function', 'fontsize', 22, 'interpreter', 'latex');
    legend([l1,l2], ["$r^{cc} = 30\%$", "$r^{cc} = 15\%$"], 'interpreter', 'latex', 'fontsize', 22, 'location', 'southeast');
 saveas(fig, ['figs/Figure8_value.png'])
          
            

fig = figure; grid on; hold on;
    bgrid = [-3.5:0.01:5];
    l1 = plot(bgrid, interp1(bH, VH(:,2), bgrid) - interp1(bL, VL(:,2), bgrid), 'linewidth', 3.5, 'color', 'black');
    xlim([-0.3 0.3+eps]); ylim([-0.1; 0.0001]);
    set(gca, 'fontsize', 18);
    xlabel('Liquid Wealth ($b$)', 'fontsize', 22, 'interpreter', 'latex'); 
    ylabel('Value Function Difference', 'fontsize', 22, 'interpreter', 'latex');
saveas(fig, ['figs/Figure8_value_difference.png'])
